package com.aizuda.boot.modules.business.comment.controller;


import com.aizuda.boot.modules.business.comment.entity.dto.CommentSaveReq;
import com.aizuda.boot.modules.business.comment.entity.vo.CommentRespDTO;
import com.aizuda.boot.modules.business.comment.service.CommentService;
import com.aizuda.boot.modules.business.contract.dto.SingleResponse;

import com.aizuda.core.api.ApiController;
import com.baomidou.kisso.annotation.LoginIgnore;
import com.baomidou.kisso.annotation.Permission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * @author yangshen
 * @version 1.0
 * @date 2025/5/22
 */

@RestController
@RequestMapping("comment")
@Tag(description = "/comment",name = "评论管理接口")
@Slf4j
public class CommentController extends ApiController {

    @Resource
    private CommentService commentService;

    /**
     * 根据合同id获取评论列表
     * @param contractId 合同ID
     * @return 评论列表
     */
    @GetMapping("/list/{contractId}")
    @Operation(summary = "查询合同评论列表", description = "根据合同ID获取评论树（含附件和子评论）")
    public SingleResponse<List<CommentRespDTO>> getCommentList(@PathVariable Integer contractId) {
        System.out.println("=== CommentController.getCommentList 被调用，contractId: " + contractId);

        List<CommentRespDTO> comments = commentService.getCommentTreeByContractId(contractId);
        System.out.println("=== Service返回结果: " + comments);

        SingleResponse<List<CommentRespDTO>> response = SingleResponse.of(comments);
        System.out.println("=== Controller返回结果: " + response);
        
        return response;
    }

    /**
     * 根据合同id和评论类型获取评论列表
     * @param contractId 合同ID
     * @param commentType 评论类型 0-协商评论 1-审批评论
     * @return 评论列表
     */
    @GetMapping("/list/{contractId}/type/{commentType}")
    @Operation(summary = "根据类型查询合同评论列表", description = "根据合同ID和评论类型获取评论树（含附件和子评论）")
    public SingleResponse<List<CommentRespDTO>> getCommentListByType(
            @PathVariable Integer contractId,
            @PathVariable @Parameter(description = "评论类型：0-协商评论，1-审批评论") Integer commentType) {
        System.out.println("=== CommentController.getCommentListByType 被调用，contractId: " + contractId + ", commentType: " + commentType);

        List<CommentRespDTO> comments = commentService.getCommentTreeByContractIdAndType(contractId, commentType);
        System.out.println("=== Service返回结果: " + comments);

        SingleResponse<List<CommentRespDTO>> response = SingleResponse.of(comments);
        System.out.println("=== Controller返回结果: " + response);
        
        return response;
    }

    /**
     * 保存评论及附件
     * 注意：用户ID和评论时间由系统自动设置，无需在请求中传递
     * @param req 评论保存请求
     * @return 操作是否成功
     */
    @PostMapping("/save")
    @Operation(summary="保存评论及附件", description = "保存评论及附件，用户ID和评论时间由系统自动设置")
    public boolean saveComment(@RequestBody CommentSaveReq req) {
        try {
            commentService.saveCommentWithAttachments(req);
            return true;
        } catch (Exception e) {
            log.error("保存评论失败", e);
            return false;
        }
    }

}
